package com.ahuan.utils;

import com.ahuan.domain.UserTest;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableSet;

import javax.management.ImmutableDescriptor;
import java.util.ArrayList;
import java.util.List;

public class JsonUtil {

    public static void main(String[] args) {
//        UserTest user = new UserTest(1,"张三");
//        String userStr = JsonUtil.toJson(user);
//        System.out.println(userStr);
//        UserTest user = JsonUtil.fromJson("{\"id\":1,\"name\":\"张三\"}",UserTest.class);
//        System.out.println(user.toString());

        List<String> list = new ArrayList<>();
        boolean b = list.removeIf(String::isEmpty);
        boolean c = list.removeIf(x -> x.isEmpty());
        ImmutableSet<String> strings = ImmutableSet.copyOf(list);

    }

    private static final ObjectMapper objectMapper = new ObjectMapper();

    // 序列化：将 Java 对象转换为 JSON 字符串
    public static String toJson(Object object) {

        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 反序列化：将 JSON 字符串转换为指定类型的 Java 对象
    public static <T> T fromJson(String json, Class<T> clazz) {
        try {
            return objectMapper.readValue(json, clazz);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }
}

